<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class menu extends Admin_Controller {
	var $theme_data = array(
	'layout_main' => 'layout',
	'left' => 'include/leftcp',
    );
	var $model = "mmenu";
	var $view = "menu";
	var $resource = "";
	
	function __construct() {
        parent::__construct();
        $this->load->model($this->model);
		$this->_model = $this->{$this->model};
		$this->resCode = $this->_model->resCode;
        $this->url_index = admin_url() . $this->view . '/index';
        $this->folder_upload = "dataupload/".$this->view."/";
        $this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
		$this->resource = $this->mresource->get($this->resCode,'RES_CODE');
    }
	
	public function index($menu = 0) {
		$this->check_permission();
        $data = array();
		$post = $this->input->post();
		if (isset($post['menu'])) {
			redirect($this->url_index .'/'. $post['menu']);
		}
		$data['parents'] = $this->_model->key_value($this->_model->get_parent(), 'men_name');
		$data['parents'][''] = 'None';
		$data['menu'] = $menu;
		if ($menu) 
			$data['edit'] = $this->_model->get($menu);
        $this->theme_data["main"] = $this->view."/index";
        $this->template->viewAdmin($this->theme_data, $data);
    }
	
	public function edit($editID = 0) {
		$this->check_permission($editID);
        $data = array();
        $post = $this->input->post();
        $data['edit'] = $this->_model->get($editID);
        if ($post) {
			if ($editID > 0) {
				$post['edit']['cat_time_create'] = time();
                $this->_model->update($editID, $this->_model->primaryKey, $post['edit']);
            }
			else{
				$editID = $this->_model->insert($post['edit']);
			}
        }
		
		redirect($this->url_index .'/'. $editID);
	}
	
	public function edit_child() {
		$result = array(
			'status' => 0,
			'msg' => 'Chưa xử lí yêu cầu'
		);
		$data = array();
		$data['men_name'] = $this->input->post('men_name');
		$data['men_type'] = $this->input->post('men_type');
		$data['men_value'] = $this->input->post('men_value');
		$men_id =  $this->input->post('men_id');
		if (
			$data['men_name']
			&& $data['men_type']
			&& $data['men_value']
		) {
			if ($men_id) {
				//update
				$this->update($this->input->post(), $men_id);
				
				$result['status'] = 1;
				$result['msg'] = 'Đã cập nhật';
			}else {
				//add
				$id = $this->add($this->input->post());
				$result['status'] = 2;
				$result['msg'] = 'Đã thêm';
				$result['men_id'] = $id;
			}
		}else {
			$result['status'] = -1;
			$result['msg'] = 'Không đủ tham số';
		}
		echo json_encode($result);
		die;
	}
	
	function add_url($parent = 0){
		$name = $this->input->post("men_name");
		$link = $this->input->post("men_link");
		$type = $this->input->post("men_type");
		//$parent = $this->input->post("men_parent");
		if($type == 'url') {
			$this->mmenu->insert(array(
				'men_name'=>$name,
				'men_type'=>'url',
				'men_parent'=>$parent,
				'men_value'=>$link,
			));
			
		}
	}
}